<template>
  <div v-loading="loading" class="app-container">
    <div class="head-container">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item name="1">
          <template slot="title">
            <i class="el-icon-search" />搜索栏
          </template>
          <el-form :model="params" class="searchform" :inline="true" size="mini" label-width="auto">
            <el-form-item label="暂停原因">
              <el-input v-model="params.suspendReason" placeholder="模糊搜索" clearable style="width: 200px;" />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" style="float:right;margin-right:0px;" icon="el-icon-refresh" round @click="resetForm">重置</el-button>
              <el-button size="mini" type="primary" style="float:right;margin-right:0px;" icon="el-icon-search" round @click="toQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>
    <div style="padding-top:5px">
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-plus" round @click="toinfo(null)">新增计划</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-plus" round @click="toBatch">批量新增</el-button>
      <el-table ref="dataTab" :data="data" stripe size="small" border style="width: 100%;" @header-dragend="colChange">
        <el-table-column :show-overflow-tooltip="true" label="开始时间" prop="beginDate" :width="colWidths.beginDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.beginDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="结束时间" prop="endDate" :width="colWidths.endDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="暂停原因" prop="suspendReason" :width="colWidths.suspendReason" />
        <el-table-column :show-overflow-tooltip="true" label="创建人" prop="createdBy" :width="colWidths.createdBy" />
        <el-table-column :show-overflow-tooltip="true" label="创建时间" prop="createdTime" :width="colWidths.createdTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" round @click="toinfo(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" round @click="doDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        :page-sizes="[10,50,100,500]"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <infoForm ref="infoForm" :parent="parent" />
    <batchForm ref="batchForm" :parent="parent" />
  </div>
</template>

<script>
import initData from '@/utils/initData'
import { parseTime } from '@/utils/index'
import infoForm from './info'
import batchForm from './batch'
import { deletePlan } from '@/api/suspend'
import { Message } from 'element-ui'

export default {
  components: { infoForm, batchForm },
  mixins: [initData],
  data() {
    return {
      parent: this,
      loading: false,
      activeName: '1'
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    beforeInit() {
      this.url = '/suspend/queryList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
    },
    // 查询栏重置触发
    resetForm() {
      this.params = {}
    },
    // 弹出详情页
    toinfo(val) {
      const _infoForm = this.$refs.infoForm
      if (val) {
        _infoForm.suspendTime = [val.beginDate, val.endDate]
        _infoForm.param = val
      } else {
        _infoForm.suspendTime = []
        _infoForm.param = {}
      }
      _infoForm.dialog = true
    },
    toBatch() {
      const _batchForm = this.$refs.batchForm
      _batchForm.param.startTime = parseTime(new Date())
      _batchForm.param.overTime = parseTime(new Date())
      _batchForm.suspendTime = [new Date(), new Date()]
      _batchForm.dialog = true
    },
    // 删除操作
    doDelete(val) {
      this.$confirm('确认删除该计划?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deletePlan(val).then(res => {
          this.toQuery()
          Message({
            message: '操作成功。',
            type: 'success',
            duration: 3 * 1000
          })
          this.loading = false
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    }
  }
}
</script>
